﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;

namespace Mole
{

    public partial class FrmMain : Form
    {

        public FrmMain()
        {
            InitializeComponent();

            dockPanelMain.DockLeftPortion = 300;

            LoadMessageForms();

            if (CheckDependency())
            {
                LoadForms();
            }
        }

        private Boolean CheckDependency()
        {
            Boolean value = Dependency.Check(Application.StartupPath + "\\Dependencies");

            if (value == false)
            {
                FrmDependencies frmdep = new FrmDependencies();

                String textmsgpath = Application.StartupPath + "\\Dependencies\\README.rtf";

                
                if (System.IO.File.Exists(textmsgpath))
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(textmsgpath);
                    frmdep.LoadText(file);
                }
                else
                {
                    frmdep.LoadText("Dependencies are missing, please see documentation for more information.");
                }

                DialogResult dr = frmdep.ShowDialog();
            }

            return value;
        }

        private void LoadMessageForms()
        {
            string[] resource = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            resource.Count();
            
            FrmMessage frmError = new FrmMessage();
            frmError.Text = String.Format("{0} {1}", "0", "Errors");
            frmError.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mole.Icons_error.ico"));
            frmError._errors = true;
            frmError.Show(this.dockPanelMain);

            FrmMessage frmWarning = new FrmMessage();
            frmWarning.Text = String.Format("{0} {1}", "0", "Warnings");
            frmWarning.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mole.Icons_warning.ico"));
            frmWarning._warnings = true;
            frmWarning.Show(this.dockPanelMain);

            FrmMessage frmMessage = new FrmMessage();
            frmMessage.Text = string.Format("{0} {1}", "0", "Messages");
            frmMessage.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mole.Icons_info.ico"));
            frmMessage._info = true;
            frmMessage.Show(this.dockPanelMain);
        }

        private void LoadForms()
        {
            FrmInputURL frmInputURL = new FrmInputURL();
            frmInputURL._dockPanelMain = dockPanelMain;
            frmInputURL.Show(dockPanelMain);

        }

        #region click actions
        private void tsmFileExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsmInput_Click(object sender, EventArgs e)
        {
            LoadForms();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmSettings frmSettings = new FrmSettings();
            frmSettings._dockPanelMain = dockPanelMain;
            frmSettings.Show(dockPanelMain);
        }

        private void tsmLoadSession_Click(object sender, EventArgs e)
        {

            try
            {
                UrlSession session;

                using (OpenFileDialog open = new OpenFileDialog())
                {
                    open.InitialDirectory = Application.StartupPath;
                    open.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
                    open.Title = "Open Session XML";
                    if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        session = UrlSession.LoadXml(open.OpenFile());

                        FrmSiteScan frmScan = new FrmSiteScan(session);
                        frmScan.Show(dockPanelMain);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                MessageBox.Show(String.Format("Error opening session - {0}", ex.Message), "Open", MessageBoxButtons.OK);
            }

            
        }

        private void tsmAbout_Click(object sender, EventArgs e)
        {
            using (FrmAbout about = new FrmAbout())
            {
                about.ShowDialog();
            }
        }
        #endregion


    }
}
